/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AppServices;

import Facades.EnterpriseFacadeRemote;
import Facades.TypeFacadeRemote;
import VOs.Enterprise;
import VOs.Type;
import java.math.BigDecimal;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Collection;
import javax.naming.InitialContext;

/**
 *
 * @author Camilo
 */
public class EnterpriseManage
{
    public static EnterpriseFacadeRemote getStartContext() throws Exception
    {
        InitialContext ctx = new InitialContext();
        EnterpriseFacadeRemote currentRemoteBean = (EnterpriseFacadeRemote) ctx.lookup("Facades.EnterpriseFacadeRemote");
        return currentRemoteBean;
    }

    public static TypeFacadeRemote getTypeStartContext() throws Exception
    {
        InitialContext ctx = new InitialContext();
        TypeFacadeRemote currentRemoteBean = (TypeFacadeRemote) ctx.lookup("Facades.TypeFacadeRemote");
        return currentRemoteBean;
    }

    public static boolean editEnterprise(String idEnterprise, String newUrl, String newPhone, String newIdType)
    {
        Type newType = null;

        Enterprise currentEnterprise = null;

        try
        {
            currentEnterprise = getStartContext().findid(idEnterprise);
        }
        catch (Exception e)
        {
            return false;
        }

        //Verificar que la empresa que se va a editar exista
        if (currentEnterprise == null)
        {
            return false;
        }

        //Cambia cada propiedad
        if (!(newUrl.equals("")))
        {
            currentEnterprise.setUrl(newUrl);
        }

        if (!(newPhone.equals("")))
        {
            currentEnterprise.setPhone(newPhone);
        }

        if (!(newIdType.equals("")))
        {
            try
            {
                newType = getTypeStartContext().findid(Integer.parseInt(newIdType));
            }
            catch (Exception e)
            {
                return false;
            }

            if (newType == null)
            {
                return false;
            }

            currentEnterprise.setFkType(newType);
        }

        try
        {
            getStartContext().edit(currentEnterprise);
        }
        catch (Exception e)
        {
            return false;
        }

        return true;
    }

    public static Enterprise getById(String referenceId) throws Exception
    {
        return getStartContext().findid(referenceId);
    }

    public static List<Enterprise> getByType(String referenceIdType, int codePage, int pageLength) throws Exception
    {
        Collection<Enterprise> wholeCollection = getStartContext().findAll();
        LinkedList<Enterprise> currentList = new LinkedList<Enterprise>();
        int startData = (codePage-1)*pageLength;
        int currentData = 1;

        Iterator<Enterprise> currentInterator = wholeCollection.iterator();
        Enterprise currentEnterprise;

        BigDecimal currentIdType = BigDecimal.valueOf(Long.parseLong(referenceIdType));

        while(currentInterator.hasNext())
        {
            currentEnterprise = currentInterator.next();

            if (currentEnterprise.getFkType() != null)
            {
                if (currentEnterprise.getFkType().getId().equals(currentIdType))
                {
                    if (pageLength == 0)
                    {
                        currentList.add(currentEnterprise);
                    }
                    else
                    {
                        if ((currentData >= startData) && (currentData <= (startData + pageLength) ))
                        {
                            currentList.add(currentEnterprise);
                            currentData++;
                        }

                        if (currentData > (startData + pageLength))
                        {
                            break;
                        }
                    }
                }
            }

        }

        return currentList;
    }

}
